package cn.iocoder.yudao.module.dify.controller.admin.taskprocessingrecords;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dify.controller.admin.taskprocessingrecords.vo.*;
import cn.iocoder.yudao.module.dify.dal.dataobject.taskprocessingrecords.TaskProcessingRecordsDO;
import cn.iocoder.yudao.module.dify.service.taskprocessingrecords.TaskProcessingRecordsService;

@Tag(name = "管理后台 - 用户调用dify任务工作流处理记录")
@RestController
@RequestMapping("/dify/task-processing-records")
@Validated
public class TaskProcessingRecordsController {

    @Resource
    private TaskProcessingRecordsService taskProcessingRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建用户调用dify任务工作流处理记录")
    @PreAuthorize("@ss.hasPermission('dify:task-processing-records:create')")
    public CommonResult<Long> createTaskProcessingRecords(@Valid @RequestBody TaskProcessingRecordsSaveReqVO createReqVO) {
        return success(taskProcessingRecordsService.createTaskProcessingRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户调用dify任务工作流处理记录")
    @PreAuthorize("@ss.hasPermission('dify:task-processing-records:update')")
    public CommonResult<Boolean> updateTaskProcessingRecords(@Valid @RequestBody TaskProcessingRecordsSaveReqVO updateReqVO) {
        taskProcessingRecordsService.updateTaskProcessingRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户调用dify任务工作流处理记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dify:task-processing-records:delete')")
    public CommonResult<Boolean> deleteTaskProcessingRecords(@RequestParam("id") Long id) {
        taskProcessingRecordsService.deleteTaskProcessingRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户调用dify任务工作流处理记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dify:task-processing-records:query')")
    public CommonResult<TaskProcessingRecordsRespVO> getTaskProcessingRecords(@RequestParam("id") Long id) {
        TaskProcessingRecordsDO taskProcessingRecords = taskProcessingRecordsService.getTaskProcessingRecords(id);
        return success(BeanUtils.toBean(taskProcessingRecords, TaskProcessingRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户调用dify任务工作流处理记录分页")
    @PreAuthorize("@ss.hasPermission('dify:task-processing-records:query')")
    public CommonResult<PageResult<TaskProcessingRecordsRespVO>> getTaskProcessingRecordsPage(@Valid TaskProcessingRecordsPageReqVO pageReqVO) {
        PageResult<TaskProcessingRecordsDO> pageResult = taskProcessingRecordsService.getTaskProcessingRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskProcessingRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户调用dify任务工作流处理记录 Excel")
    @PreAuthorize("@ss.hasPermission('dify:task-processing-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskProcessingRecordsExcel(@Valid TaskProcessingRecordsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskProcessingRecordsDO> list = taskProcessingRecordsService.getTaskProcessingRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户调用dify任务工作流处理记录.xls", "数据", TaskProcessingRecordsRespVO.class,
                        BeanUtils.toBean(list, TaskProcessingRecordsRespVO.class));
    }

}